﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace liuzw.utils
{
    internal static class JsonExtensions
    {
        public static string ToJson(this object obj)
        {
            string jsonValue = "";
            try
            {
                if (obj != null)
                    jsonValue = JsonConvert.SerializeObject(obj);
            }
            catch (Exception ex)
            {
                throw new Exception("Json序列化异常", ex);
            }
            return jsonValue;
        }
        public static dynamic ToDynamic(this string jsonValue)
        {
            try
            {
                if (!string.IsNullOrEmpty(jsonValue))
                    return JsonConvert.DeserializeObject<dynamic>(jsonValue);
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw new Exception("Json反序列化异常", ex);
            }
        }
       
    }
}
